import 'package:boss/ui/widget/calendar/util/date_util.dart';
import 'package:flutter/material.dart';

class CalendarProvider extends ChangeNotifier {
  DateTime minDateTime;
  DateTime maxDateTime;
  DateTime startDateTime;
  DateTime endDateTime;
  int currentPageIndex = 0;
  int maxPage;

  DateTime nowDateTime;

  PageController pageController;

  String get titleText {
    var dateTime = DateUtils.addMonthsToMonthDate(minDateTime, currentPageIndex);
    return '${dateTime.year}年${dateTime.month}月';
  }

  CalendarProvider({
    this.minDateTime,
    this.maxDateTime,
    this.startDateTime,
    this.endDateTime,
  }) {
    nowDateTime = DateTime.now();
    if (minDateTime == null) {
      minDateTime = nowDateTime;
    }

    var prevMonths = DateUtils.monthDelta(minDateTime, startDateTime ?? nowDateTime);
    currentPageIndex = prevMonths;

    if (maxDateTime != null) {
      maxPage = DateUtils.monthDelta(minDateTime, maxDateTime) + 1;
    }
  }

  setPageIndex(int index) {
    currentPageIndex = index;
    notifyListeners();
  }

  setDateTime(DateTime dateTime) {
    if ((startDateTime == null || dateTime.isBefore(startDateTime)) && endDateTime == null) {
      startDateTime = dateTime;
      notifyListeners();
    } else if (startDateTime != null && endDateTime == null && dateTime.isAfter(startDateTime)) {
      endDateTime = dateTime;
      notifyListeners();
    } else if (startDateTime != null && endDateTime != null) {
      startDateTime = dateTime;
      endDateTime = null;
      notifyListeners();
    }
  }

  nextPage() {
    pageController.nextPage(duration: Duration(milliseconds: 150), curve: Curves.decelerate);
  }

  prevPage() {
    pageController.previousPage(duration: Duration(milliseconds: 150), curve: Curves.decelerate);
  }
}
